#include "qpushbox.h"
#include <QPainter>
#include <QPushButton>
#include <QDebug>
#include <dataconfig.h>
#include <iostream>
#include <QKeyEvent>
#include <QMessageBox>
#include <QLabel>
#include <QPoint>
#include <QPen>

QPushBox::QPushBox(QWidget *parent) :
    QWidget(parent)
{
    //推箱子界面设计
    setFixedSize(760, 560); //设置固定大小
    setWindowTitle("推箱子"); //设置标题
    setWindowIcon(QIcon(":/down.png")); //设置图标

    //关卡地图数据初始化
    initLevel(minLevel);

    //切换关卡按钮及其他按钮设计：
    //上一关卡按钮
    QPushButton * lastBtn = new QPushButton("上一关卡");
    lastBtn->setParent(this);
    lastBtn->resize(150,60);
    lastBtn->setStyleSheet("background-color:orange");
    lastBtn->setFont(QFont("华文新魏", 12));
    lastBtn->move(590, 40);
    //监听点击信号
    connect(lastBtn, &QPushButton::clicked,[=](){
        if(levelindex!=minLevel)
        {
            initLevel(levelindex-1);
            update();
        }
    });

    //下一关卡按钮
    QPushButton * nextBtn = new QPushButton("下一关卡");
    nextBtn->setParent(this);
    nextBtn->resize(150,60);
    nextBtn->setStyleSheet("background-color:orange");
    nextBtn->setFont(QFont("华文新魏", 12));
    nextBtn->move(590, 180);
    //监听点击信号
    connect(nextBtn, &QPushButton::clicked,[=](){
        if(levelindex!=maxLevel)
        {
            initLevel(levelindex+1);
            update();
        }
    });

    //重新开始本关按钮
    QPushButton * restartBtn = new QPushButton("重玩本关");
    restartBtn->setParent(this);
    restartBtn->resize(150,60);
    restartBtn->setStyleSheet("background-color:orange");
    restartBtn->setFont(QFont("华文新魏", 12));
    restartBtn->move(590, 320);
    //监听点击信号
    connect(restartBtn, &QPushButton::clicked,[=](){
        initLevel(levelindex);
        update();
    });

    //游戏说明按钮
    QPushButton * noteBtn = new QPushButton("游戏说明");
    noteBtn->setParent(this);
    noteBtn->resize(150,60);
    noteBtn->setStyleSheet("background-color:orange");
    noteBtn->setFont(QFont("华文新魏", 12));
    noteBtn->move(590, 460);
    //监听点击信号
    connect(noteBtn, &QPushButton::clicked,[=](){
        QString str = "用键盘上的上、下、左、右键移动小人，把箱子全部推到小球的位置即可过关。箱子只可向前推，不能往后拉，并且小人一次只能推动一个箱子";
        QMessageBox::information(this,"游戏说明",str);
    });

}

QPushBox::~QPushBox()
{

}

void QPushBox::initLevel(int level) // 使用level参数初始化对应关卡数据
{
    //判断是否越界
    if(level < minLevel) level = minLevel;
    if(level > maxLevel) level = maxLevel;
    levelindex = level;
    this->person_position_x = -1;
    this->person_position_y = -1;
    //获胜标志
    isWin = false;
    //人物朝向为下
    hero_stat = DIR_DOWN;
    //移动次数置0
    moveTimes = 0;
    //读取关卡数据
    for(int i = 0; i < 16; i++)
    {
        for(int j = 0; j < 16; j++)
        {
            gameArray[i][j] = config.mData[level][j][i];
            posArray[i][j] = config.mData[level][j][i];
        }
    }
}